package com.qding.park.manager.timer.schedule.job;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.springframework.stereotype.Service;

import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import com.qding.park.common.core.cache.service.CacheServiceFacade;
import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.common.core.redis.SerializeUtils;
import com.qding.park.manager.pojo.TbInvitecard;
import com.qding.park.manager.pojo.queue.QueueMsg;
import com.qding.park.manager.pojo.queue.QueueTypeEnum;
import com.qding.park.manager.timer.ParkTimerApplication;
import com.qding.park.manager.timer.conf.TaskConfig;

/**
 * @ClassName ReserveCheckJob
 * @Description TODO(邀请单超时检查任务)
 * @author Administrator
 * @Date 2016年9月15日 下午3:53:58
 * @version 1.0.0
 */
@Service
@DisconfUpdateService( classes={TaskConfig.class},itemKeys = { "task.InviteCheckJob.name" ,"task.InviteCheckJob.taskJobClass","task.InviteCheckJob.cronTime"})
public class InviteCheckJob extends AbstractJob implements IDisconfUpdate{
	private static final Log _log = LogFactory.getLog(InviteCheckJob.class);

	@Override
	public void execJob(JobExecutionContext context) {
		JobKey jobKey = context.getJobDetail().getKey();
		
		_log.info("InviteCheckJob starting");
		CacheServiceFacade cacheServiceFacade = ParkTimerApplication.ctxUtil.getApplicationContext()
				.getBean(CacheServiceFacade.class);
		
		List<TbInvitecard> tbInvitecards= cacheServiceFacade.invite_timeout_monitor_list();
		// 得到redis client
//		JedisClient jedisClient = ParkTimerApplication.ctxUtil.getApplicationContext().getBean(JedisClient.class);
		
		//获取邀请单的　　监控列表
//		Map<String, String> mset=jedisClient.hgetAll(CacheKeyThree.MONITOR_INVITE_KEY);
		
		for(TbInvitecard tbInvitecard:tbInvitecards){
			Date endTime=tbInvitecard.getEndTime();
			Date now=new Date();
			if(now.getTime()>endTime.getTime()){
				//超时了
				//发送队列消息
				QueueMsg  msg=new QueueMsg();
				
				msg.setEventType(QueueTypeEnum.INVITE_TIMEOUT.getType());
				TbInvitecard data=new TbInvitecard();
				data.setId(tbInvitecard.getId());
				msg.setData(data);
				
//				String msgStr=SerializeUtils.serialize(msg);
				
				//邀请单超时　消息
				cacheServiceFacade.msg_queue_push(msg);
				//删除监控
				cacheServiceFacade.invite_timeout_monitor_delete(tbInvitecard.getId());
			}
		}
//		if(MapUtils.isNotEmpty(mset)){
//			for(Map.Entry<String, String> entry:mset.entrySet()){
//				String key=entry.getValue();
//				String value=entry.getValue();
//				
//				TbInvitecard tbInvitecard=SerializeUtils.unSerialize(value, TbInvitecard.class);
//				Date endTime=tbInvitecard.getEndTime();
//				Date now=new Date();
//				if(now.getTime()>endTime.getTime()){
//					//超时了
//					//发送队列消息
//					QueueMsg  msg=new QueueMsg();
//					
//					msg.setEventType(QueueTypeEnum.INVITE_TIMEOUT.getType());
//					TbInvitecard data=new TbInvitecard();
//					data.setId(tbInvitecard.getId());
//					msg.setData(data);
//					
//					String msgStr=SerializeUtils.serialize(msg);
//					
//					//邀请单超时　消息
//					jedisClient.lpush(CacheKeyThree.QUEUE_ALL, msgStr);
//				}
//			}
//		}
		
		_log.info("邀请单超时检查任务: " + jobKey + " executing at " + new Date());

	}

	@Override
	public void reload() throws Exception {
		_log.info(TaskConfig.getTask_InviteCheckJob_name());
		String taskName=TaskConfig.getTask_InviteCheckJob_name();
		String cronTime=TaskConfig.getTask_InviteCheckJob_cronTime();
		super.reloadTask(taskName,cronTime);
		
	}
}
